import http from './interface'
import config from '../config.js'
/**
 * 将业务所有接口统一起来便于维护
 * 如果项目很大可以将 url 独立成文件，接口分成不同的模块
 * 
 */
// 请求数据处理
function serialize(obj, prefix) {
	const str = []
	let p
	if (obj.length === 0) {
		//  str.push(encodeURIComponent(prefix) + '=1')  
	} else {
		for (p in obj) {
			if (obj.hasOwnProperty(p)) {
				const k = prefix ? prefix + '[' + p + ']' : p
				let v = obj[p]
				if (v instanceof Date) {
					v = parseTime(v)
				}
				str.push((v !== null && typeof v === 'object') ? serialize(v, k) : encodeURIComponent(k) + '=' + encodeURIComponent(
					v))
			}
		}
	}
	return str.join('&')
}
export const setPath = (pname) => {
	var path = config[pname];
	var obj = {
		apiPost: (url, data) => {
			//设置请求前拦截器
			http.interceptor.request = (config) => {
				let url = config.url
				// 对部分接口需要将token放到请求头中
				let reg = /insertStuTime/g
				if (reg.test(url)) {
					config.header = {
						"token": uni.getStorageSync('token'),
						"reqMethod": 2,
						"Content-Type": "application/json"
					}
				}
			}
			//设置请求结束后拦截器
			http.interceptor.response = (response) => {
				// console.log('个性化response....')
				//判断返回状态 执行相应操作
				return response;
			}
			let dataurl = path + url

			let token = {
				// 'k_token': uni.getStorageSync('token') || uni.getStorageSync('_token'),
				// 'token': uni.getStorageSync('token') || uni.getStorageSync('_token')
				'k_token':'c7f104e641d6b910427df20a268643e6',
				'token': 'c7f104e641d6b910427df20a268643e6'
			}
			data = Object.assign({}, data, token)
			data = serialize(data)
			return http.request({
				url: dataurl,
				data
			})
		},
		apifile: (url, data) => {
			//设置请求结束后拦截器
			http.interceptor.response = (response) => {
				// console.log('个性化response....')
				//判断返回状态 执行相应操作
				return response;
			}
			let dataurl = path + url
			let token = {
				// 'k_token': uni.getStorageSync('token') || uni.getStorageSync('_token'),
				// 'token': uni.getStorageSync('token') || uni.getStorageSync('_token')
				'k_token':'c7f104e641d6b910427df20a268643e6',
				'token': 'c7f104e641d6b910427df20a268643e6'
			}
			data = Object.assign({}, data, token)
			return http.uploadfiles({
				url: dataurl,
				data
			})
		}
	}
	return obj;
}

// 默认全部导出  import api from '@/common/vmeitime-http/'
export default {
	setPath
}
